package com.itany.corejava.code15_stream;

import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年12月15日 15:48
 */
public class Test03_终止操作 {
    private static List<Integer> list = Arrays.asList(1, 2, 3, 4, 1, 13, 74, 26, 4, 9, 2);

    public static void main(String[] args) {
        // allMatchTest();
        // anyMatchTest();
        // noneMatchTest();
        // findFirstTest();
        // findAnyTest();
        // countTest();
        // maxTest();
        // minTest();
        // reduceTest();
        collectTest();
    }

    public static void allMatchTest() {
        //检查流中所有元素是否都比0大
        boolean flag = list.stream().allMatch(i -> i > 0);
        System.out.println(flag);
    }

    public static void anyMatchTest() {
        //检查流中是否包含大于10的元素
        boolean flag = list.stream().anyMatch(i -> i > 10);
        System.out.println(flag);
    }

    public static void noneMatchTest() {
        //检查流中是否都不大于10
        boolean flag = list.stream().noneMatch(i -> i < 0);
        System.out.println(flag);
    }

    public static void findFirstTest() {
        list = new ArrayList<>();
        Optional<Integer> first = list.stream().findFirst();
        //如果有值

        if (first.isPresent()) {

            Integer num = first.get();
            System.out.println(num);
        } else {
            //如果没有值，直接取出来会抛异常，可以为其设置一个默认值
            Integer num = first.orElse(0);
            System.out.println(num);
        }
    }

    public static void findAnyTest() {


        // Optional<Integer> any=list.stream().findAny();
        // System.out.println(any.get());
        // Optional<Integer> any2=list.stream().findAny();
        // System.out.println(any.get());
        // Optional<Integer> any3=list.stream().findAny();
        // System.out.println(any.get());


        //并行流
        // Optional<Integer> any4=list.parallelStream().findAny();
        // System.out.println(any4.get());
        // Optional<Integer> any5=list.parallelStream().findAny();
        // System.out.println(any5.get());
        // Optional<Integer> any6=list.parallelStream().findAny();
        // System.out.println(any6.get());

        new Thread() {
            @Override
            public void run() {
                Optional<Integer> any4 = list.parallelStream().findAny();
                System.out.println(any4.get());
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                Optional<Integer> any5 = list.parallelStream().findAny();
                System.out.println(any5.get());
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                Optional<Integer> any6 = list.parallelStream().findAny();
                System.out.println(any6.get());
            }
        }.start();

    }

    public static void countTest() {
        long count = list.stream().count();
        System.out.println("流中的元素的数量:" + count);
    }

    public static void maxTest() {
        Optional<Integer> max = list.stream().max((o1, o2) -> o1 - o2);
        System.out.println(max.get());
        Optional<Integer> max2 = list.stream().max((o1, o2) -> o2 - o1);
        System.out.println(max2.get());

    }

    public static void minTest() {
        Optional<Integer> min = list.stream().min((o1, o2) -> o1 - o2);
        System.out.println(min.get());
        Optional<Integer> min2 = list.stream().min((o1, o2) -> o2 - o1);
        System.out.println(min2.get());

    }

    public static void reduceTest() {

        // 归并方法,如果是+,求和,如果是-,所有数字相减去
        Optional<Integer> reduce = list.stream().reduce(new BinaryOperator<Integer>() {
            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer - integer2;
            }
        });
        System.out.println(reduce.get());
        Optional<Integer> reduce2 = list.stream().reduce((a, b) -> a - b);
        System.out.println(reduce2.get());


    }


    public static void collectTest() {
        //将Stream流转换成List集合
        List<Integer> list2 = list.stream().collect(Collectors.toList());
        System.out.println(list2);


    }
}
